package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.model.Train;
import com.ybkj.daijia.server.service.TrainService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Shine
 */
@Controller
@RequestMapping(value = "train")
public class TrainController {


    @Autowired
    private TrainService trainService;

    @RequestMapping("create")
    public @ResponseBody
    Result<Train> create(Train train) {

        Subject subject = SecurityUtils.getSubject();

        Result<Train> result = new Result<Train>();
        result.setSuccess(false);

        if (subject.isPermitted("zhuan")) {
            trainService.insert(train);

            result.setSuccess(true);
        } else {
            result.setMessage("您没有该权限");
        }
        return result;

    }


    @RequestMapping("trainData")
    public @ResponseBody
    Page<Train> driverListData(
        @RequestParam("driverId") Long driverId) {
        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("zhuan")) {

            Page<Train> result = trainService.queryByEmploy(driverId);

            return result;
        } else {
            throw new NoPermissionException();
        }

    }


    @RequestMapping(value = "update", method = RequestMethod.POST)
    public @ResponseBody
    Result<Train> update(Train train) throws ParseException {

        Subject subject = SecurityUtils.getSubject();

        Result<Train> result = new Result<Train>();
        result.setSuccess(false);

        if (subject.isPermitted("zhuan")) {

            Train findTrain = trainService.findOne(train.getId());

            if (findTrain != null) {

                findTrain.setDriverId(train.getDriverId());
                findTrain.setCourse(train.getCourse());
                findTrain.setTrainTime(train.getTrainTime());
                findTrain.setBeginTime(train.getBeginTime());
                findTrain.setEndTime(train.getEndTime());
                findTrain.setTrainDays(train.getTrainDays());
                trainService.update(findTrain);

            } else {
                result.setMessage("没有可以操作的数据");
            }
        } else {
            result.setMessage("您没有该权限");
        }
        return result;

    }


    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public Map<String, Object> delete(
        @RequestParam(required = true, value = "id") Long id) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("zhuan")) {
            Map<String, Object> map = new HashMap<String, Object>();
            try {
                trainService.delete(id);
                map.put("success", true);
                map.put("message", "公告删除成功");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return map;
        } else {
            throw new NoPermissionException();
        }
    }

    @org.springframework.web.bind.annotation.InitBinder
    public void InitBinder(WebDataBinder dataBinder) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

    }
}

